/*
 * SdlEventsReader.cpp
 *
 *  Created on: Nov 14, 2011
 *      Author: gonzalo
 */

#include "SdlEventsReader.h"
#include "../../SDLtoolkit/SDLEngine.h"

#define FPS 40

SdlEventsReader::SdlEventsReader(ColaMensajesSDL* colaALlenar,void* engine) {
	this->colaALlenar = colaALlenar;
	this->engine = engine;
	this->running = true;
}

SdlEventsReader::~SdlEventsReader() {
}

void SdlEventsReader::terminar(){
	this->running = false;
}

void *SdlEventsReader::run(){

	Uint32 waittime = 1000.0f/FPS;
	Uint32 framestarttime = 0;
	Sint32 delaytime;

	Event* event;
	while(running){

		while((event = ((SDLEngine*)engine)->pollEvent())){
			if (event) {
				colaALlenar->setEvent(event);
			}
		}
		delaytime = waittime - (SDL_GetTicks() - framestarttime);
		if(delaytime > 0)
			SDL_Delay((Uint32)delaytime);
		framestarttime = SDL_GetTicks();

	}

	return NULL;
}



